#include <istream>
#include <fstream>
#include "contacts.pb.h"

void PrintInput(const c_contacts::Contacts contacts)
{
    for(int i = 0;i<contacts.person_size();i++)
    {
        const c_contacts::People& person = contacts.person(i);
        std::cout << "-------------------联系人：" << i+1 << "---------------------" << std::endl;
        std::cout << "联系人姓名：" << person.name() << std::endl;
        std::cout << "联系人年龄：" << person.age() << std::endl;


        for(int j = 0;j<person.phone_size();j++)
        {
            c_contacts::People_Phone phone = person.phone(j);
            std::cout << "联系人电话" << j+1 << ": " << phone.num() << std::endl;
        }

        //查看people消息中的未知字段
        const google::protobuf::Reflection * reflection = c_contacts::People::GetReflection();
        const google::protobuf::UnknownFieldSet & unknown = reflection->GetUnknownFields(person);
        for(int k = 0;k<unknown.field_count();k++)
        {
            const google::protobuf::UnknownField& unknownfield= unknown.field(k);
            std::cout << "未知字段：" << k+1 << "编号: " << unknownfield.number() << " ";
            switch(unknownfield.type())
            {
                case google::protobuf::UnknownField::TYPE_VARINT:
                    std::cout << "值: " << unknownfield.varint() << std::endl;
                    break;
                case google::protobuf::UnknownField::TYPE_FIXED32:
                    std::cout << "值: " << unknownfield.fixed32() << std::endl;
                    break;
                //........
            }
        }
    }


}
int main()
{
    std::ifstream input("../contacts.bin",std::ios::in | std::ios::binary);
    c_contacts::Contacts contacts;
    if(!contacts.ParseFromIstream(&input))
    {
        std::cerr << "Parse error" << std::endl;
        input.close();
        return -1;
    }


    PrintInput(contacts);
}